<?php
namespace app\fsadmin\controller;

use think\Controller;

class PermissionController extends BaseController
{
    private $per_l;

    public function _initialize()
    {
        parent::_initialize();
        $this->per_l = model('PermissionLogic', "logic");
    }

/*****************************管理员************************************************/
    public function admin()
    {
        if ($this->request->isAjax()) {
            $params = $this->request->param();
            $rs= $this->per_l->getAdminList($params);
            $rs['status']       = 200;
            ajax_return($rs);
        }

        $role = $this->per_l->getRoles();
        $this->assign("role",$role['data']['list']);
        return $this->fetch();

    }

    public function saveadmin()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->param();
            $rs   = $this->per_l->saveAdmin($data);
            model("log")->saveLog(["content"=>"更新管理员","datas"=> serialize($data)]);
            ajax_return($rs);
        }
    }

    public function addadmin()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->param();
            $rs   = $this->per_l->addAdmin($data);
            model("log")->saveLog(["content"=>"添加管理员","datas"=> serialize($data)]);
            ajax_return($rs);
        }
    }


    public function deladmin()
    {
        if ($this->request->isAjax()) {
            $params = $this->request->only("id");
            $rs     = $this->per_l->delAdmin($params['id']);
             model("log")->saveLog(["content"=>"删除管理员","datas"=> serialize($data)]);
            ajax_return($rs);
        }
    }


    /****************************************权限模块*******************************/
    public function rule()
    {
        if ($this->request->isAjax()) {
            $params = $this->request->param();
            $rs= $this->per_l->getRule($params);
            $rs['status']       = 200;
            ajax_return($rs);
        }
        return $this->fetch();
    }
    

    public function saverule()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->except("level");
            $rs   = $this->per_l->saveRule($data);
             model("log")->saveLog(["content"=>"更新规则","datas"=> serialize($data)]);
            ajax_return($rs);
        }
    }

    public function addrule()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->except("level");
            $rs   = $this->per_l->addRule($data);
            model("log")->saveLog(["content"=>"添加规则","datas"=> serialize($data)]);
            ajax_return($rs);
        }
    }

    public function delrule()
    {
        if ($this->request->isAjax()) {
            $params = $this->request->only("id");
            $rs   = $this->per_l->delRule($params['id']);
             model("log")->saveLog(["content"=>"删除规则","datas"=> serialize($params['id'])]);
            ajax_return($rs);
        }
    }

    /**********************************************角色模块*********************************/
    public function role()
    {
        if ($this->request->isAjax()) {
            $params = $this->request->param();
            $rs   = $this->per_l->getRoles($params);
            ajax_return($rs);
        }
        return $this->fetch();
    }


    public function addrole()
    {
        if ($this->request->isAjax()) {
            $rs   = $this->per_l->getTreeRules();
            ajax_return($rs);
        }
        return $this->fetch();
    }

    public function editrole()
    {
        return $this->fetch();
    }

    public function saverole()
    {
        if ($this->request->isAjax()) {
            $params = $this->request->param();
            $rs   = $this->per_l->saveRole($params);
            model("log")->saveLog(["content"=>"更新角色","datas"=> serialize($params)]);
            ajax_return($rs);
        }
    }

    public function delrole()
    {
        if ($this->request->isAjax()) {
            $params = $this->request->only("id");
            $rs   = $this->per_l->delRole($params['id']);
             model("log")->saveLog(["content"=>"删除角色","datas"=> serialize($params['id'])]);
            ajax_return($rs);
        }
    }

}
